package com.koron.standard.jobStock.maintain.excel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.koron.standard.deviceClasssify.service.TypeService;
import com.koron.standard.jobStock.maintain.bean.dto.MaintainDTO;
import com.koron.standard.jobStock.maintain.bean.dto.MaintainItemDTO;
import com.koron.standard.jobStock.maintain.bean.po.MaintainItemParams;
import com.koron.standard.jobStock.maintain.service.MaintainService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/12/25
 */
public class YcMaintainListener extends MaintainListener {
    private MaintainService maintainService;

    private MaintainDTO maintainDTO;
    private boolean isItem;

    public YcMaintainListener(TypeService typeService, MaintainService maintainService) {
        super(typeService, maintainService);
        this.maintainService = maintainService;
    }

    public String getCodeByLetter(String letter) {
        // 分类编码 > 层级编号 > 字母编号
        // 分类编码和字母编号二选一，两者都有时以分类编码为准
        if (letter != null) {
            char[] chars = letter.toCharArray();
            StringBuilder sb = new StringBuilder();
            for (char aChar : chars) {
                sb.append(String.format("%03d", aChar - 'A' + 1));
            }
            return sb.toString();
        }
        return null;
    }


    @Override
    public String getIdByTypeCode(String letter) {
        String typeCode = letter;
        if (!StrUtil.isNumeric(letter)) {
            typeCode = getCodeByLetter(letter);
        }
        return super.getIdByTypeCode(typeCode);
    }

    public String saveData() {
        if (maintainDTO != null) {
            maintainService.saveDto(maintainDTO);
            return maintainDTO.getId();
        }
        return null;
    }

    public void resetData() {
        isItem = false;
        maintainDTO = new MaintainDTO();
        maintainDTO.setItemDTOs(new ArrayList<>());
    }


    @Override
    public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
        if (isSkipped()) {
            return;
        }
        if ("维护项目".equals(map.get(0))) {
            isItem = true;
            return;
        }
        if ("维护标准名称".equals(map.get(0))) {
            // 作业标准名称
            if (StringUtils.isEmpty(map.get(2))) {
                setErrorMessage(getCurrentSheetName()+ "的作业标准名称不能为空");
                throw new ExcelAnalysisStopException();
            }
            maintainDTO.setName(map.get(2));
            // 设备分类ID
            if (StringUtils.isEmpty(map.get(4))) {
                setErrorMessage(getCurrentSheetName()+ "的设备分类ID不能为空");
                throw new ExcelAnalysisStopException();
            }
            String typeId = getIdByTypeCode(map.get(4));
            maintainDTO.setTypeId(typeId);
            // 作业步骤层数
            maintainDTO.setStepLevel(1);
            maintainDTO.setAppid(getAppId());
            maintainDTO.setStatus(0);
            maintainDTO.setPublicStatus(0);
        } else if (isItem && map.get(1) != null) {
            // 没有.则在第一层
            if (!map.get(1).contains(".")) {
                // 加入最外层
                MaintainItemDTO maintainItemDTO = getByMap(map);
                if (maintainDTO.getStepLevel() > 1) {
                    maintainItemDTO.setChildren(new ArrayList<>());
                }
                maintainDTO.getItemDTOs().add(maintainItemDTO);
            } else {
                // 长度为3则在第二层
                List<MaintainItemDTO> itemDTOs = maintainDTO.getItemDTOs();
                // 获取最后一层
                MaintainItemDTO itemDTO = itemDTOs.get(itemDTOs.size() - 1);
                if (CollectionUtils.isEmpty(itemDTO.getChildren())) {
                    itemDTO.setChildren(new ArrayList<>());
                }
                itemDTO.getChildren().add(getByMap(map));
            }
        }

    }

    private List<MaintainItemParams> resolveItemParams(String str) {
        List<String[]> strings = resolveItemStr(str, "\n");
        return strings.stream().map(e -> {
            MaintainItemParams maintainItemParams = new MaintainItemParams();
            maintainItemParams.setName(e[0]);
            maintainItemParams.setUnit(e[1]);
            return maintainItemParams;
        }).collect(Collectors.toList());
    }

    private MaintainItemDTO getByMap(Map<Integer, String> map) {
        MaintainItemDTO maintainItemDTO = new MaintainItemDTO();
        maintainItemDTO.setContext(map.get(2));
        maintainItemDTO.setContextStandard(map.get(3));
        maintainItemDTO.setRisk(map.get(4));
        maintainItemDTO.setLevel(1);
        maintainItemDTO.setItemParams(resolveItemParams(map.get(5)));
        return maintainItemDTO;
    }

}
